package by.demchenko.sample.webapp.page.admin;

import by.demchenko.sample.datamodel.Course;
import by.demchenko.sample.datamodel.Person;
import by.demchenko.sample.datamodel.Section;
import by.demchenko.sample.datamodel.enums.UserType;
import by.demchenko.sample.services.CourseService;
import by.demchenko.sample.services.PersonService;
import by.demchenko.sample.services.SectionService;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.*;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;

import javax.inject.Inject;
import java.util.List;

public class CourseEditPage extends BaseAdminPage {

    @Inject
    CourseService courseService;

    @Inject
    PersonService personService;

    @Inject
    SectionService sectionService;

    public CourseEditPage(Course course) {
        super();

        add(new Label("headerTitle", getString("label.course")));
        add(new Label("subtitle", getString("label.editor")));
        add(new FeedbackPanel("feedback"));

        Form<Course> form = new Form<>("form", new CompoundPropertyModel<>(course));

        final TextField<String> tfName = new TextField<>("name");
        tfName.add(new PropertyValidator<>());
        form.add(tfName);

        List<Person> teachersList = personService.getPersonsByType(UserType.TEACHER, false);
        final DropDownChoice<Person> tfPerson = new DropDownChoice<>(
                "teacher",
                new PropertyModel<>(course, "teacher"),
                teachersList,
                new ChoiceRenderer<>("getFullName", "id"));
        tfPerson.add(new PropertyValidator<>());
        tfPerson.setRequired(true);
        form.add(tfPerson);

        List<Section> sectionsList = sectionService.getAll();
        final DropDownChoice<Section> tfSection = new DropDownChoice<>(
                "section",
                new PropertyModel<>(course, "section"),
                sectionsList,
                new ChoiceRenderer<>("name", "id"));
        tfSection.add(new PropertyValidator<>());
        tfSection.setRequired(true);
        form.add(tfSection);

        final TextField<String> tfDescription = new TextField<>("description");
        tfDescription.add(new PropertyValidator<>());
        form.add(tfDescription);

        form.add(new SubmitLink("sumbit-link") {
            @Override
            public void onSubmit() {
                super.onSubmit();
                courseService.saveOrUpdate(course);
                CoursesPage page = new CoursesPage();
                setResponsePage(page);
            }

            @Override
            public void onError() {
                super.onError();
            }
        });

        form.add(new Link<Void>("back-link") {

            @Override
            public void onClick() {
                setResponsePage(new CoursesPage());
            }
        });

        add(form);
    }
}
